﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RecommenderSystem.misc;


namespace RecommenderSystem.algorithms
{
    public abstract class MemoryBased: Algoritm
    {

        public MemoryBased(Dictionary<string, User> users, ICollection<IDataRecord> dataset):base(users, dataset){}

        public override double predictRating(string userID, string itemID)
        {

            if (_users == null || !_users.ContainsKey(userID))
            {
                return -1;
            }

            User user = _users[userID];
            double numerator = 0.0;
            double denominator = 0.0;
            double usersSimilarity = 0.0;
            //Traverse over all users that rated item sIID , except the user himself.
            foreach (User otherUser in _users.Values.Where(x => !x.getUserId().Equals(user.getUserId()) && x.getItemRate(itemID) > -1))
            {
                usersSimilarity = calculateSimilarity(user, otherUser);    
                if (usersSimilarity > Config.SIMILARITY_ENV)
                {
                    numerator += (usersSimilarity * otherUser.getItemRate(itemID));
                    denominator += usersSimilarity;
                }
            }
            
            return denominator != 0.0 ? numerator / denominator : 0d ;
        }

 
    }

}
